import base64
import os
import librosa


def dbImageBase64ToBytes(dbImageBase64):
    return base64.b64decode(bytes(eval(dbImageBase64)))


def dbImageBase64ListToBytesList(dbImageBase64List):
    return [base64.b64decode(bytes(dbImageBase64)) for dbImageBase64 in eval(dbImageBase64List)]


def imageBytesToBase64(imageBytes):
    return base64.b64encode(imageBytes)


def imageBytesListToBase64List(imageBytesList):
    return [imageBytesToBase64(imageBytes) for imageBytes in imageBytesList]


def getAudioDuration(filePath):
    """
    获取mp3/wav音频文件时长
    :param filePath: 文件地址
    :return: 时长(s)
    """
    duration = librosa.get_duration(filename=filePath)
    return duration


def joinAndMakePath(*args):
    dirPath = os.path.join(*args)
    if "." in args[-1]:
        dirPath = os.path.join(*args[:-1])
    os.makedirs(dirPath, exist_ok=True)
    return os.path.join(*args)